package com.system.manager.modules.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.manager.common.entity.PageDTO;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.authorized.service.IUserService;
import com.system.manager.modules.risk.RiskErrorCode;
import com.system.manager.modules.risk.dto.RiskRiskVo;
import com.system.manager.modules.risk.dto.RiskTypeVo;
import com.system.manager.modules.risk.dto.RiskVo;
import com.system.manager.modules.risk.entity.Risk;
import com.system.manager.modules.risk.entity.RiskType;
import com.system.manager.modules.risk.entity.TbExam;
import com.system.manager.modules.risk.entity.TbFlow;
import com.system.manager.modules.risk.exception.riskException;
import com.system.manager.modules.risk.mapper.RiskMapper;
import com.system.manager.modules.risk.mapper.RiskTypeMapper;
import com.system.manager.modules.risk.mapper.TbFlowMapper;
import com.system.manager.modules.risk.service.IRiskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.manager.modules.risk.service.IRiskTypeService;
import com.system.manager.modules.risk.vo.UserDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.system.manager.modules.risk.RiskErrorCode.NON_FINDFLOW_RISK;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-16
 */
@Service
public class RiskServiceImpl extends ServiceImpl<RiskMapper, Risk> implements IRiskService {

    @Autowired
    private RiskTypeMapper riskTypeMapper;
    @Autowired
    private RiskMapper riskMapper;
    @Autowired
    private TbFlowMapper tbFlowMapper;
    @Autowired
    private IRiskTypeService iRiskTypeService;
    //风险录入
    @Override
    public void insertRisk(RiskVo riskVo) {
        Risk risk = new Risk();
        //哪个业务中的风险
        risk.setFId(riskVo.getFId());
        //风险发布人
        risk.setRIssue(riskVo.getRIssue());
        //风险发布人的手机号
        risk.setRPhone(riskVo.getRPhone());

        //风险名
        Integer rKey = riskVo.getRKey();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("rt_id",rKey);
        RiskType riskType = riskTypeMapper.selectOne(wrapper);
        risk.setRName(riskType.getRtState());
        //风险类型
        risk.setRKey(rKey);
        //创建时间
        Date date = new Date();
        risk.setCreateTime(date);

        riskMapper.insert(risk);
    }
    //查询风险
    @Override
    public UserDate<Risk> findRisk(Integer currPage, Integer size) {
        QueryWrapper wrapper = null;
        IPage<Risk> riskIPage = new Page<>(currPage,size);
        page(riskIPage,wrapper);

        UserDate<Risk> userDate = new UserDate<>();
        userDate.setCode(0);
        userDate.setMsg("风险");
        userDate.setCount((int)riskIPage.getTotal());
        userDate.setData(riskIPage.getRecords());
        return userDate;
    }

    //风险页面添加风险
    @Override
    public void riskInsertRisk(RiskRiskVo riskRiskVo) {
        Risk risk = new Risk();
        /**处理业务*/
        Integer fId = riskRiskVo.getRiskFlow();
        /*QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("f_name",rFlow);
        TbFlow tbFlow = tbFlowMapper.selectOne(wrapper);
        //判断是否有该业务
        if(tbFlow == null){
            //没有该业务
            throw new riskException(RiskErrorCode.NON_FINDFLOW_RISK);
        }
        //有该业务
        Integer fId = tbFlow.getFId();*/
        risk.setFId(fId);
        /**风险名*/
        Integer riskTypeId = riskRiskVo.getRiskType();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("rt_id",riskTypeId);
        RiskType riskType = riskTypeMapper.selectOne(wrapper);
        String rName = riskType.getRtState();
        risk.setRName(rName);
        /**风险联系人*/
        risk.setRIssue(riskRiskVo.getRIssue());
        /**联系方式*/
        risk.setRPhone(riskRiskVo.getRPhone());
        /**
         * 风险的分类
         */
        risk.setRKey(riskTypeId);
        /**
         * 风险发布时间
         */
        Date date = new Date();
        risk.setCreateTime(date);
       /* QueryWrapper wrapperType = new QueryWrapper();
        wrapperType.eq("rt_state",rName);
        RiskType riskType = riskTypeMapper.selectOne(wrapperType);
        //判断是否有该类型的风险
        if(riskType == null){
            RiskTypeVo riskTypeVo = new RiskTypeVo();
            riskTypeVo.setRtState(rName);
            iRiskTypeService.insert(riskTypeVo);
            RiskType riskType1 = riskTypeMapper.selectOne(wrapperType);
            Integer rtId = riskType.getRtId();
            risk.setRKey(rtId);
            Date date = new Date();
            risk.setCreateTime(date);
        }else {
            Integer rtId = riskType.getRtId();
            risk.setRKey(rtId);
            Date date = new Date();
            risk.setUpdateTime(date);
        }*/
        riskMapper.insert(risk);
    }

    @Override
    public PageDTO<Risk> selectRiskCondition(Integer currPage, Integer size, String fName, String rName, String rIssue) {
        Page<User> userPage = new Page<>(currPage, size);
        Integer fId;
        if(fName != null){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("f_name",fName);
            TbFlow tbFlow = tbFlowMapper.selectOne(wrapper);
            if(tbFlow == null){
                fId = null;
            }else{
                fId = tbFlow.getFId();
            }
        }else {
            fId = null;
        }
        // 1.根据条件查询出当前所有的对象，并进行分页封装
        List<Risk> riskList = new ArrayList<>();
        try {
            riskList = riskMapper.selectRiskCondition(userPage, fId, rName, rIssue);
        } catch (Exception e) {
            e.printStackTrace();
            throw new riskException(RiskErrorCode.NON_FIND_EXAM_RISK);
        }
        // 2.将查询出来的数据返回给前端
        PageDTO<Risk> riskPageDTO = new PageDTO<>();
        riskPageDTO.setData(riskList);
        riskPageDTO.setCount(userPage.getTotal());
        return riskPageDTO;
    }

}
